cmake_minimum_required(VERSION 2.8)
#project(muduo C CXX)
project(muduo)

#set(CMAKE_CXX_STANDARD 11)

# enable test
# if not, CMake would ignore add_test()
enable_testing()

message(STATUS "Build on " ${CMAKE_SYSTEM})
message(STATUS "Current CMAKE Version:${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION}")

if (NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Release")
endif ()

# only build examples if this is the main project
if (CMAKE_PROJECT_NAME STREQUAL "muduo")
    message(STATUS "Build examples option on")
    option(MUDUO_BUILD_EXAMPLES "Build Muduo examples" ON)
endif ()

# set list variable CXX_FLAGS
set(CXX_FLAGS
        -g
        # -DVALGRIND
        -DCHECK_PTHREAD_RETURN_VALUE
        -D_FILE_OFFSET_BITS=64
        # compile option
        # https://zhuanlan.zhihu.com/p/393419013
        # https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

        # This enables all the warnings about constructions that some users consider questionable,
        # and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros.
#        -Wall

        # This enables some extra warning flags that are not enabled by -Wall.
        -Wextra
        # Make all warnings into errors.
        -Werror
        # Warn for implicit conversions that may alter a value.
        -Wconversion
        # cancel warning -Wunused-parameter: "unused parameter ..."
        -Wno-unused-parameter
        # Warn for old style cast (C style)
        -Wold-style-cast
        # Warn for overloaded-virtual function
        -Woverloaded-virtual
        # Warn for pointer arith
        -Wpointer-arith
        # Warn whenever a local variable or type declaration shadows another variable,
        # parameter, type, class member (in C++), or instance variable (in Objective-C)
        # or whenever a built-in function is shadowed.
        -Wshadow
        # When compiling C, give string constants the type const char[length] so that
        # copying the address of one into a non-const char * pointer produces a warning.
        -Wwrite-strings
        # https://stackoverflow.com/questions/52653025/why-is-march-native-used-so-rarely
        -march=native
        # The same as -MM( ignores the dependencies caused by # include),
        # but the output will be imported into the. d file
        # https://programmer.group/gcc-m-mm-mmd-mf-mt.html
        # -MMD
        # option specifying the required version of ISO C or C++
        -std=c++11
        # link option, indicate add all of symbols to .dynsym table for invoking dlopen() or backtrace()
        -rdynamic
        )

if (CMAKE_BUILD_BITS EQUAL 32)
    list(APPEND CXX_FLAGS "-m32")
endif ()
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    list(APPEND CXX_FLAGS "-Wno-null-dereference")
    list(APPEND CXX_FLAGS "-Wno-sign-conversion")
    list(APPEND CXX_FLAGS "-Wno-unused-local-typedef")
    list(APPEND CXX_FLAGS "-Wthread-safety")
    list(REMOVE_ITEM CXX_FLAGS "-rdynamic")
endif ()
# convert list "aa;bb;cc;" to "aa bb cc", i.e. ";" => " "
string(REPLACE ";" " " CMAKE_CXX_FLAGS "${CXX_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "-O0")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")

# set output directory of exe and library
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

# find_package()
# https://www.cnblogs.com/penuel/p/13983503.html
# Must find out the package, or else stop cmake project building
find_package(Boost REQUIRED)
find_package(Protobuf)
find_package(CURL)
#find_package(BOOSTTEST_LIBRARY NAMES boost_unit_test_framework)
find_library(BOOSTTEST_LIBRARY NAMES boost_unit_test_framework)
find_library(Boost NAMES boost_unit_test_framework)
find_library(BOOSTPO_LIBRARY NAMES boost_program_options)
find_library(BOOSTSYSTEM_LIBRARY NAMES boost_system)
#find_package(Boost COMPONENTS unit_test_framework REQUIRED)

if (CARES_INCLUDE_DIR AND CARES_LIBRARY)
    message(STATUS "found cares")
endif ()
if (NOT CURL_FOUND)
    message(STATUS "not found curl")
endif ()
if (PROTOBUF_FOUND)
    message(STATUS "found protobuf")
endif()
if (BOOSTTEST_LIBRARY)
    message(STATUS "found boost test")
else ()
    message(WARNING "not found boost test")
endif()

# header search path
include_directories(${Boost_INCLUDE_DIRS})
include_directories(${PROJECT_SOURCE_DIR})

string(TOUPPER ${CMAKE_BUILD_TYPE} BUILD_TYPE)
message(STATUS "CXX_FLAGS = " ${CMAKE_CXX_FLAGS} " " ${CMAKE_CXX_FLAGS_${BUILD_TYPE}})

add_subdirectory(muduo/base)
add_subdirectory(muduo/net)
add_subdirectory(ftpserver)

if (MUDUO_BUILD_EXAMPLES)
    add_subdirectory(examples)
endif ()
